package be.rivendale.renderer.mk2;

import be.rivendale.material.Color;
import be.rivendale.renderer.mk2.data.Queue;
import be.rivendale.renderer.mk2.data.Rectangle;
import be.rivendale.renderer.mk2.data.Segment;
import be.rivendale.renderer.mk2.local.MultiThreadedProducer;
import be.rivendale.renderer.mk2.local.RenderThread;
import be.rivendale.renderer.mk2.remote.NodeDetectorThread;
import be.rivendale.renderer.mk2.remote.Transmitter;
import be.rivendale.renderer.mk2.window.WindowConsumer;

public class Master {
    public static void main(String[] arguments) {
//        launchSlave();
        launchMaster();
    }

    private static void launchMaster() {
        Queue<Segment> resultQueue = new Queue<Segment>();

        ConsumerThread consumerThread = new ConsumerThread(resultQueue);
        WindowConsumer windowConsumer = new WindowConsumer();
        consumerThread.addConsumer(windowConsumer);

        final LoadBalancer loadBalancer = new LoadBalancer();
        loadBalancer.addProducer(new MultiThreadedProducer(2, resultQueue) {
            @Override
            protected RenderThread createRenderThread(Queue<Rectangle> inputQueue, Queue<Segment> outputQueue) {
                return new RenderThread(inputQueue, outputQueue, Color.GREEN.multiply(0.25), 0);
            }
        });
        consumerThread.addConsumer(loadBalancer);
        consumerThread.addConsumer(new Commander(loadBalancer, windowConsumer));

        NodeDetectorThread detector = new NodeDetectorThread(loadBalancer, resultQueue);
        consumerThread.start();
    }

    private static void launchSlave() {
        Queue<Segment> resultQueue = new Queue<Segment>();

        ConsumerThread consumerThread = new ConsumerThread(resultQueue);
        LoadBalancer loadBalancer = new LoadBalancer();
        loadBalancer.addProducer(new MultiThreadedProducer(2, resultQueue) {
            @Override
            protected RenderThread createRenderThread(Queue<Rectangle> inputQueue, Queue<Segment> outputQueue) {
                return new RenderThread(inputQueue, outputQueue, Color.RED.multiply(0.25), 0);
            }
        });
        consumerThread.addConsumer(loadBalancer);

        consumerThread.addConsumer(new Transmitter(loadBalancer));

        consumerThread.start();
    }

}
